<?php
App::uses('AppModel', 'Model');
class Category extends AppModel {

	public $name = 'Category';
	public $useTable = 'categories_multi';

	/**
	 * Get category
	 *
	 * @param string $cat_type
	 * @throws NotFoundException
	 * @return mixed
	 */
	public function getListCategoryType($cat_type = null) {
		try{
			if ($cat_type != null){
				$sql = " AND cat_type = '" . $cat_type . "'";
			} else {
				$sql = '';
			}
			$results = $this->query("SELECT id,cat_name,cat_order,cat_type,cat_parent_id,cat_level FROM categories_multi WHERE cat_active = 1 " . $sql . "ORDER BY sort_key ASC");
			return  $results;

		} catch (Exception $e) {
			throw new NotFoundException(__('Invalid category'));
		}

	}

	/**
	 * Get parent category
	 * @param string $id
	 * @throws NotFoundException
	 * @return Ambigous <multitype:, NULL>
	 */
	public function getParentCategory($parent_id = null){
		$query = array(
				'conditions' => array(
						'id' => $parent_id,
						'cat_active' => 1
				),
		);
		try{
			$cat_parent = $this->find('first', $query);
			return $cat_parent;

		} catch (Exception $e) {
			throw new NotFoundException(__('Invalid category'));
		}
	}

}